import android.content.Context
import android.graphics.Bitmap
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.yoomiito.app.R
import com.yoomiito.app.model.sdd.ShareMsg
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageUtils
import com.yoomiito.app.utils.UserInfoConfig
import de.hdodenhof.circleimageview.CircleImageView

class SddShareView_2 (val context: Context){
    val view = LayoutInflater.from(context).inflate(R.layout.sdd_share_ad_2,null,false)
    val userPhoto = view.findViewById<CircleImageView>(R.id.userPhoto)
    val userName = view.findViewById<TextView>(R.id.userName)
    val qrCode = view.findViewById<ImageView>(R.id.QRCode)
    var userPhotoSuccess = false
    var qrCodeSuccess = false
    var makeViewListener:(View, Bitmap)->Unit={ _, _ ->}

    fun setViewData(msg: ShareMsg) {
        userName.text = UserInfoConfig.getUser().name
            Glide.with(context).asBitmap().load(UserInfoConfig.getUser().headimgurl).into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    userPhoto.setImageBitmap(resource)
                    userPhotoSuccess = true
                    makePic()
                }
            })
            Glide.with(context).asBitmap().load(msg.url).into(object : SimpleTarget<Bitmap>() {
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    qrCode.setImageBitmap(resource)
                    qrCodeSuccess = true
                    makePic()
                }
            })

    }

    private fun makePic(){
        ImageUtils.layoutView(view, DensityUtil.dp2px(375f), DensityUtil.dp2px(620f))
        if (userPhotoSuccess&&qrCodeSuccess){
            makeViewListener(view,ImageUtils.viewSaveToImage(view))
        }
    }
}